package com.taobao.txc.common.a;

import com.taobao.txc.common.LoggerInit;
import com.taobao.txc.common.LoggerWrap;
import com.taobao.txc.common.config.o;
import com.taobao.txc.common.j;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;
import org.apache.commons.lang.StringUtils;

public class b
{
  private static final LoggerWrap a = LoggerInit.logger;
  private static int b = 16;
  private static int c = 200000;
  private SimpleDateFormat d = new SimpleDateFormat("HH:mm:ss ");
  private SimpleDateFormat e = new SimpleDateFormat(".yyyy-MM-dd");
  private ConcurrentHashMap<String, e> f = new ConcurrentHashMap(b * 3);
  private StringBuilder[] g;
  private ScheduledExecutorService h;
  private ReentrantLock i = new ReentrantLock();
  private HashMap<String, LinkedBlockingQueue<String>> j = new HashMap();
  private String k;
  
  public b()
  {
    String str = System.getProperty("user.home");
    if (StringUtils.isEmpty(str)) {
      str = "/home/admin";
    }
    this.k = (str + File.separator + "logs" + File.separator + "txc" + File.separator + "analyze" + File.separator + "index");
    File localFile = new File(this.k);
    if (localFile.exists())
    {
      if (!localFile.isDirectory()) {
        a.error(null, "the analyze index directory is invalid - " + localFile.getAbsolutePath());
      }
    }
    else if (!localFile.mkdirs()) {
      a.error(null, "failed to create the analyze index directory - " + localFile.getAbsolutePath());
    }
  }
  
  void a(String paramString)
  {
    if (!a.a().b()) {
      return;
    }
    if (this.h == null) {
      synchronized (this)
      {
        if (this.h == null)
        {
          this.h = Executors.newScheduledThreadPool(2);
          this.h.scheduleWithFixedDelay(new c(this), 10L, 60L, TimeUnit.SECONDS);
          this.h.scheduleAtFixedRate(new d(this), 1L, 1L, TimeUnit.HOURS);
        }
      }
    }
    ??? = j.b(paramString);
    LinkedBlockingQueue localLinkedBlockingQueue = (LinkedBlockingQueue)this.j.get(???);
    if (localLinkedBlockingQueue == null)
    {
      this.i.lock();
      try
      {
        localLinkedBlockingQueue = (LinkedBlockingQueue)this.j.get(???);
        if (localLinkedBlockingQueue == null)
        {
          localLinkedBlockingQueue = new LinkedBlockingQueue(c);
          this.j.put(???, localLinkedBlockingQueue);
        }
      }
      finally
      {
        this.i.unlock();
      }
    }
    localLinkedBlockingQueue.offer(paramString);
  }
  
  private void a(String paramString1, String paramString2)
  {
    e locale = (e)this.f.get(paramString1);
    if (locale == null)
    {
      this.f.putIfAbsent(paramString1, new e(this, paramString1));
      locale = (e)this.f.get(paramString1);
    }
    try
    {
      locale.a(paramString2.getBytes("utf-8"));
    }
    catch (Exception localException)
    {
      a.a("IO failed", "can't write to file " + paramString1, localException);
      return;
    }
  }
  
  private void b()
  {
    if (this.j.size() == 0) {
      return;
    }
    File localFile = new File(this.k);
    if ((!localFile.exists()) || (!localFile.isDirectory()))
    {
      Iterator localIterator1 = this.j.keySet().iterator();
      while (localIterator1.hasNext())
      {
        str1 = (String)localIterator1.next();
        localObject = (LinkedBlockingQueue)this.j.get(str1);
        if (localObject != null) {
          ((LinkedBlockingQueue)localObject).clear();
        }
      }
      return;
    }
    if (this.g == null)
    {
      this.g = new StringBuilder[b];
      for (int m = 0; m < b; m++) {
        this.g[m] = new StringBuilder(8000);
      }
    }
    Date localDate = new Date();
    String str1 = this.d.format(localDate);
    Object localObject = this.e.format(localDate);
    Iterator localIterator2 = this.j.keySet().iterator();
    while (localIterator2.hasNext())
    {
      String str2 = (String)localIterator2.next();
      String str3 = str2.replace(':', '_');
      LinkedBlockingQueue localLinkedBlockingQueue = (LinkedBlockingQueue)this.j.get(str2);
      if (localLinkedBlockingQueue != null)
      {
        int n = localLinkedBlockingQueue.size();
        if (n == 0) {
          return;
        }
        String str4;
        for (int i2 = 0; i2 < n; i2++)
        {
          str4 = (String)localLinkedBlockingQueue.poll();
          if (str4 == null) {
            break;
          }
          long l = j.a(str4);
          i1 = (int)(l % b);
          if (this.g[i1].length() == 0) {
            this.g[i1].append(str1).append(l);
          } else {
            this.g[i1].append(' ').append(l);
          }
        }
        for (int i1 = 0; i1 < b; i1++)
        {
          StringBuilder localStringBuilder = this.g[i1];
          if (localStringBuilder.length() != 0)
          {
            localStringBuilder.append('\n');
            str4 = String.format("%s%s%s%s%02d%sindex%s", new Object[] { this.k, File.separator, str3, File.separator, Integer.valueOf(i1), File.separator, localObject });
            a(str4, localStringBuilder.toString());
            localStringBuilder.setLength(0);
          }
        }
      }
    }
  }
  
  private void c()
  {
    int m = o.a().d();
    d();
    a(m);
  }
  
  private void d()
  {
    Calendar localCalendar = Calendar.getInstance();
    int m = localCalendar.get(11);
    if ((m != 1) && (m != 3)) {
      return;
    }
    localCalendar.add(5, -1);
    String str1 = this.e.format(localCalendar.getTime());
    Set localSet = this.f.keySet();
    Iterator localIterator = localSet.iterator();
    while (localIterator.hasNext())
    {
      String str2 = (String)localIterator.next();
      String str3 = str2.substring(str2.length() - this.e.toPattern().length());
      if (str3.compareTo(str1) < 0)
      {
        e locale = (e)this.f.remove(str2);
        locale.a();
      }
    }
  }
  
  private void a(int paramInt)
  {
    if (paramInt < 1) {
      return;
    }
    Calendar localCalendar = Calendar.getInstance();
    int m = localCalendar.get(11);
    if ((m != 5) && (m != 21)) {
      return;
    }
    StringBuilder localStringBuilder = new StringBuilder();
    localStringBuilder.append("find ").append(this.k).append(" -mtime +").append(paramInt);
    localStringBuilder.append(" -name 'index.*' -type f -delete");
    com.taobao.txc.common.util.a locala = new com.taobao.txc.common.util.a();
    try
    {
      locala.a(new String[] { "/bin/sh", "-c", localStringBuilder.toString() }, null);
      String str = locala.a(com.taobao.txc.common.util.d.b, 10000L);
      a.info(String.format("clean index files done. cmd:[%s] res:[%s]", new Object[] { localStringBuilder.toString(), str }));
    }
    catch (Exception localException)
    {
      a.warn("clean index files failed. " + localException.getMessage());
    }
    finally
    {
      locala.a();
    }
  }
}


/* Location:           C:\Users\ibm\Downloads\txc-yun-sample (1)\txc-yun-sample\lib\txc-client-2.0.72.jar
 * Qualified Name:     com.taobao.txc.common.a.b
 * JD-Core Version:    0.7.0.1
 */